import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../../config/StyleConfig.dart';
import '../res/HDColors.dart';
import '../util/HDNavigator.dart';

class CommonWidget extends StatelessWidget {
  CommonWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container();
  }

  static AppBar getAppBar(String appBarTitle,
      {WillPopCallback? willPopCallback, List<Widget>? actions}) {
    return AppBar(
      title: Text(
        appBarTitle,
        style: const TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500),
      ),
      actions: actions,
      leading: IconButton(
        onPressed: () async {
          if (willPopCallback == null || await willPopCallback()) {
            HDNavigator.close();
          }
        },
        icon: const Icon(
          Icons.arrow_back_ios,
          size: 20.0,
        ),
      ),
      centerTitle: true,
    );
  }

  static Widget getAppBarLeading({WillPopCallback? willPopCallback}) {
    return IconButton(
      onPressed: () async {
        if (willPopCallback == null || await willPopCallback()) {
          HDNavigator.close();
        }
      },
      icon: const Icon(Icons.arrow_back_ios, size: 20.0),
    );
  }

  static Widget getFooterButton(String title, VoidCallback callback) {
    return MaterialButton(
      onPressed: callback,
      color: const Color(0xff155BD4),
      shape: const RoundedRectangleBorder(
        //边框颜色
        side: BorderSide(
          color: Colors.white,
          width: 0,
        ),
        //边框圆角
        borderRadius: BorderRadius.all(
          Radius.circular(0),
        ),
      ),
      height: 44.0,
      minWidth: double.infinity,
      child: Text(
        title,
        style: const TextStyle(
            color: Color(0xffFFFFFF),
            fontSize: 16,
            fontWeight: FontWeight.w600),
      ),
    );
  }

  static Divider getDivider() {
    return const Divider(
      height: 0.5,
      color: Color(0x1F000000),
    );
  }

  static divider(double height, {double indent = 0, double endIndent = 0}) {
    return Divider(
      height: height,
      thickness: height,
      indent: indent,
      endIndent: endIndent,
    );
  }

  static Widget getBoildDivider() {
    return Container(
      color: Color(0x1F000000),
      height: 1.5,
    );
  }

  static Widget getFormItem(title, Widget content, isVisible) {
    return Container(
      // key: Key(colName),
      height: 52,
      margin: const EdgeInsets.fromLTRB(16.0, 0.0, 16.0, 0.0),
      padding: const EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
      decoration: const BoxDecoration(
          border:
              Border(bottom: BorderSide(color: Color(0x14000000), width: 1.0))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(title,
              style: const TextStyle(
                  color: Color(0xff646566),
                  fontSize: StyleConfig.fontContentSize16)),
          const SizedBox(
            width: 16,
            height: 1,
          ),
          Expanded(child: content),
          const SizedBox(
            width: 6,
            height: 1,
          ),
          Visibility(
              visible: isVisible,
              child: const Icon(
                Icons.arrow_right,
                color: Color(0xff323233),
                size: 10,
              ))
        ],
      ),
    );
  }

  /// 搜索标题
  static searchTitle(TextEditingController? controller,
      PopupMenuItemSelected onSelected, List typeList, String typeName,
      {String searchHintText = "", onSubmitted}) {
    return Row(
      children: [
        Expanded(
            flex: 1,
            child: Visibility(
                visible: controller != null,
                child: Container(
                  margin: const EdgeInsets.fromLTRB(15, 10, 15, 10),
                  padding: const EdgeInsets.all(5),
                  decoration: BoxDecoration(
                      color: HDColors.white,
                      borderRadius: BorderRadius.circular(8)),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      InkWell(
                          onTap: () {
                            //隐藏键盘
                            SystemChannels.textInput
                                .invokeMethod('TextInput.hide');
                            if (onSubmitted != null) {
                              onSubmitted(controller!.text);
                            }
                          },
                          child: Padding(
                              padding: const EdgeInsets.fromLTRB(6, 0, 2, 0),
                              child: Image.asset(
                                  "assets/images/icon_search.webp",
                                  width: 16,
                                  height: 16))),
                      Expanded(
                          flex: 1,
                          child: TextFormField(
                            controller: controller,
                            style: const TextStyle(
                                color: HDColors.black,
                                fontSize: StyleConfig.fontContentSize16),
                            cursorColor: HDColors.black,
                            textInputAction: TextInputAction.search,
                            decoration: InputDecoration(
                              border: InputBorder.none,
                              contentPadding: const EdgeInsets.only(
                                  top: 0, bottom: 0, left: 10),
                              isDense: true,
                              hintText: searchHintText.isNotEmpty
                                  ? searchHintText
                                  : "搜索",
                              hintStyle: const TextStyle(
                                  color: HDColors.grayHint,
                                  fontSize: StyleConfig.fontContentSize16),
                            ),
                            onFieldSubmitted: onSubmitted,
                          )),
                      InkWell(
                          onTap: () {
                            SystemChannels.textInput
                                .invokeMethod('TextInput.hide');
                            controller!.clear();
                            if (onSubmitted != null) {
                              onSubmitted(controller.text);
                            }
                          },
                          child: Container(
                              margin: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                              child: const Icon(Icons.cancel,
                                  color: HDColors.grayHint, size: 20))),
                      // Container(
                      //   height: 30,
                      //   child: ElevatedButton(
                      //       onPressed: () {
                      //         SystemChannels.textInput.invokeMethod('TextInput.hide');
                      //         if (onSubmitted != null) {
                      //           onSubmitted(controller.text);
                      //         }
                      //       },
                      //       style: ButtonStyle(
                      //           backgroundColor:
                      //               MaterialStateProperty.all(HDColors.btnBgColor)),
                      //       child: const Text("搜索",
                      //           style: TextStyle(
                      //             color: HDColors.white,
                      //             fontSize: 14.0,
                      //           ))),
                      // )
                    ],
                  ),
                ))),
        Visibility(
            visible: typeList.isEmpty ? false : true,
            child: Container(
                margin: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                padding: const EdgeInsets.only(right: 15),
                child: PopupMenuButton(
                    elevation: 5,
                    offset: const Offset(0, 15),
                    position: PopupMenuPosition.under,
                    padding: const EdgeInsets.all(0),
                    onCanceled: () {
                      print("onCanceled回调");
                    },
                    onSelected: (dynamic result) => onSelected(result),
                    itemBuilder: (context) {
                      return typeList
                          .map((e) => PopupMenuItem(
                                height: 35,
                                value: e,
                                child: Text(
                                  e["name"],
                                  style: const TextStyle(
                                      fontSize: StyleConfig.fontContentSize16,
                                      color: HDColors.black),
                                  textAlign: TextAlign.center,
                                ),
                              ))
                          .toList();
                    },
                    child: Row(children: [
                      Container(
                          margin: const EdgeInsets.only(left: 5),
                          child: Text(typeName,
                              style: const TextStyle(
                                  color: HDColors.black,
                                  fontSize: StyleConfig.fontContentSize16,
                                  fontWeight: FontWeight.w600))),
                      const Icon(Icons.keyboard_arrow_down_rounded,
                          color: HDColors.black, size: 13)
                    ]))))
      ],
    );
  }

  /// item的button
  static buildItemButton(
      String title, Color fontColor, VoidCallback onPressed) {
    return TextButton(
        child: Text(title,
            style: TextStyle(fontSize: StyleConfig.fontContentSize12)),
        onPressed: onPressed,
        style: ButtonStyle(
          foregroundColor: MaterialStateProperty.all(fontColor),
          //背景颜色
          backgroundColor: MaterialStateProperty.all(Colors.white),
          //边框
          side: MaterialStateProperty.all(
            BorderSide(color: fontColor, width: 0.67),
          ),
        ));
  }

  static Container buildTag(nodestatue) {
    String text = "待开始";
    Color bgcolor = HDColors.blue;
    Color fontcolor = HDColors.blue;
    if (nodestatue == 2) {
      text = "审核不通过";
      bgcolor = HDColors.red;
      fontcolor = HDColors.red;
    }
    if (nodestatue == 4) {
      text = "转办";
      bgcolor = HDColors.orangeRed;
      fontcolor = HDColors.orangeRed;
    }
    if (nodestatue == 0) {
      text = "退回";
      bgcolor = HDColors.red;
      fontcolor = HDColors.red;
    }
    if (nodestatue == 6) {
      text = "撤回";
      bgcolor = HDColors.orangeRed;
      fontcolor = HDColors.orangeRed;
    }
    if (nodestatue == 7) {
      text = "在线";
    }
    if (text == "") {
      return Container();
    }
    return Container(
      padding: EdgeInsets.only(left: 8, right: 8),
      margin: EdgeInsets.only(left: 5, right: 5),
      decoration: BoxDecoration(
        color: bgcolor.withOpacity(0.15),
        border: Border.all(
          color: bgcolor,
          width: 1.0,
        ),
      ),
      child: Text(
        text,
        textAlign: TextAlign.center,
        style: TextStyle(
          color: fontcolor,
        ),
      ),
    );
  }

  static Container buildStatusTag(nodestatue) {
    String text = "在线";
    Color bgcolor = HDColors.blue;
    Color fontcolor = HDColors.blue;
    if (nodestatue == 0) {
      text = "离线";
      bgcolor = HDColors.signBg;
      fontcolor = HDColors.formTitleColor;
    }
    if (nodestatue == 1) {
      text = "在线";
    }
    if (nodestatue == 2) {
      text = "告警";
      bgcolor = HDColors.orangeRed;
      fontcolor = HDColors.orangeRed;
    }

    if (nodestatue == 6) {
      text = "撤回";
      bgcolor = HDColors.orangeRed;
      fontcolor = HDColors.orangeRed;
    }
    if (text == "") {
      return Container();
    }
    return Container(
      padding: EdgeInsets.only(left: 8, right: 8),
      margin: EdgeInsets.only(left: 5, right: 5),
      decoration: BoxDecoration(
        color: bgcolor.withOpacity(0.15),
        border: Border.all(
          color: bgcolor,
          width: 1.0,
        ),
      ),
      child: Text(
        text,
        textAlign: TextAlign.center,
        style: TextStyle(
          color: fontcolor,
        ),
      ),
    );
  }

  static Widget buildIconWidget({bool showIcon = true,String nodestatue = ""}) {
    Widget icon = Icon(
      Icons.circle,
      color: HDColors.blue,
      size: 17,
    );
    var tempColor = HDColors.blue;
    //待开始
    if (nodestatue == "待开始") {
      tempColor = HDColors.grayHint;
      icon = Icon(
        Icons.circle,
        color: tempColor,
        size: 17,
      );
    }
    //已完成
    if (nodestatue == "已完成") {
      tempColor = HDColors.red;
      icon = Icon(
        Icons.check_circle,
        color: tempColor,
        size: 17,
      );
    }
    //进行中
    if (nodestatue == "进行中") {
      tempColor = HDColors.blue;
      icon = Icon(
        Icons.edit_location_alt_rounded,
        color: tempColor,
        size: 17,
      );
    }
    //进行中
    if (nodestatue == "待设计") {
      tempColor = HDColors.green;
      icon = Icon(
        Icons.edit_location_alt_rounded,
        color: tempColor,
        size: 17,
      );
    }
    if (nodestatue == "草稿") {
      tempColor = HDColors.classifiedBg;
      icon = Icon(
        Icons.edit_location_alt_rounded,
        color: tempColor,
        size: 17,
      );
    }
    if (nodestatue == "待勘探") {
      tempColor = HDColors.orangeF07E2DFF;
      icon = Icon(
        Icons.edit_location_alt_rounded,
        color: tempColor,
        size: 17,
      );
    }
    if (nodestatue == "审核不通过") {
      tempColor = HDColors.red;
    }
    // 退回
    if (nodestatue == 5) {
      icon = Container(
        decoration: new BoxDecoration(
          color: HDColors.red,
          borderRadius: BorderRadius.all(Radius.circular(44.0)),
        ),
        width: 17,
        height: 17,
        child: Icon(
          Icons.arrow_back,
          color: HDColors.white,
          size: 15,
        ),
      );
    }
    return Row(
      children: [
        Visibility(
          visible: showIcon,
            child: icon),
        SizedBox(width: 5,),
        Text(nodestatue??"待开始",
          textAlign: TextAlign.center,
          style: TextStyle(
            color: tempColor,
            fontSize: StyleConfig.fontContentSize14,
          ),
        )
      ],
    );
  }

  static Widget buildBottomBtn(String title, VoidCallback onPressed) {
    return title == "底部"
        ? TextButton(
        onPressed: onPressed,
        child: Container(
          padding: const EdgeInsets.symmetric(vertical: 4),
          child: Text(title,
              style: const TextStyle(
                  color: HDColors.formTitleColor,
                  fontSize: StyleConfig.fontContentSize16)),
        ))
        : ElevatedButton(
      style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(HDColors.green)),
      onPressed: onPressed,
      child: Container(
        padding: const EdgeInsets.all(10),
        child: Text(title,
            style: const TextStyle(
                color: HDColors.white,
                fontSize: StyleConfig.fontContentSize16)),
      ),
    );
  }

  static tabBar(TabController controller, List<BadgeTab> tabs,
      {backgroundColor = HDColors.primary,
        showMore = false,
        moreWindowText,
        closeController,
        indicatorWidth = 30.0,
        indicatorHeight = 2.5,
        labelColor=HDColors.white,
        indicatorColor=HDColors.white
      }) {
    return BrnTabBar(
        labelColor: labelColor,
        indicatorColor: indicatorColor,
        indicatorWidth: indicatorWidth,
        controller: controller,
        tabs: tabs,
        mode: BrnTabBarBadgeMode.origin,
        showMore: showMore,
        labelPadding: const EdgeInsets.fromLTRB(0, 0, 20, 10),
        moreWindowText: moreWindowText,
        backgroundcolor: backgroundColor,
        closeController: closeController,
        themeData: BrnTabBarConfig(
            indicatorHeight: indicatorHeight,
            preLineTagCount: 3,
            labelStyle: BrnTextStyle(
                fontSize: 16,
                color: HDColors.blue,
                fontWeight: FontWeight.w600),
            unselectedLabelStyle: BrnTextStyle(
                fontSize: 16,
                color: HDColors.black,
                fontWeight: FontWeight.w400),
            tagSelectedTextStyle: BrnTextStyle(
                fontSize: StyleConfig.fontContentSize16,
                color: HDColors.blue,
                fontWeight: FontWeight.w600),
            tagNormalTextStyle: BrnTextStyle(
                fontSize: StyleConfig.fontContentSize16,
                color: HDColors.black,
                fontWeight: FontWeight.w400)));
  }
}
